package com.heepay.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 可以获取被spring管理的bean的工具类
 */
@Component
@Lazy(false)
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
 
    public static <T> T  getBean(String name) {
        //name表示其他要注入的注解name名
        T bean = null;
        try {
            bean = (T) context.getBean(name);
        } catch (BeansException e) {
            e.printStackTrace();
        }
        return bean;
    }
 
    public static <T> T getBean(Class<T> requiredType) {
        return context.getBean(requiredType);
    }
    
}